package com.example.framework.excel.testExcel;

/**
 * @author gx
 * @date 2021/6/11 11:01
 */

import com.example.framework.excel.enums.ExcelType;
import com.example.framework.excel.excelUtils.ExcelUtil;
import com.example.framework.excel.exmple.ManySheets;
import com.example.project.entity.GoodsBean;
import com.example.project.service.ExcelService;
import com.example.utils.date.DateUtils;
import com.example.utils.resultFormat.Result;
import com.example.utils.resultFormat.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;


/**
 * @author gx
 * @describe
 * @createTime 2019-11-01
 */
@RestController
@RequestMapping("/imp-exp-template")
@Api(value = "导入导出模板测试", description = "导入导出模板")
public class RunTest {

    @Autowired
    private ExcelService goodService;
//---------------------------------单个sheet工作簿-----------------------------------------------------------------------------
    @ApiOperation(value = "导入模板下载(单一数据)", notes = "导入模板下载(单一数据)")
    @GetMapping(value = "/single/exp/template")
    public void singleExpTemplate(HttpServletResponse response) {

        String fileName = "商品单sheet模板"+ DateUtils.fileNameFormatTime();
        ExcelUtil.exportTemplate(fileName, GoodsBean.class, ExcelType.XLS, response);

    }

    @ApiOperation(value = "数据导出(单一数据)", notes = "数据导出(单一数据)")
    @GetMapping(value = "/single/exp/data")
    public void singleExpData(HttpServletResponse response) {

        List<GoodsBean> goodsBeanInfoList = goodService.getExcelInfo();

        String fileName = "商品数据-" + DateUtils.fileNameFormatTime();
        ExcelUtil.exportExcel(fileName,goodsBeanInfoList, GoodsBean.class, ExcelType.XLS, response);
    }

    @ApiOperation(value = "数据导入(单一数据)", notes = "数据导入(单一数据)")
    @PostMapping(value = "/single/imp/data")
    public Result singleImpData(@RequestParam("file") MultipartFile file) {

        List<GoodsBean> list = ExcelUtil.importExcel(file, GoodsBean.class);
        boolean saveBatch = goodService.saveBatch(list);
        return ResultUtil.success(saveBatch);
    }

    //---------------------------------多个sheet工作簿且可以是不同的工作簿-----------------------------------------------------------------------------

    /**
     * 多个工作簿导入模板下载 没问题
     * @param response
     */
    @ApiOperation(value = "导入模板下载(多种数据)", notes = "导入模板下载(多种数据)")
    @GetMapping(value = "/multiple/exp/template")
    public void multipleExpTemplate(HttpServletResponse response) {
        String fileName = "商品多sheet模板(单一数据)";
        ExcelUtil.exportMultipleTemplate(fileName, ManySheets.class, response);
    }

    /**
     * 多个工作簿同时导入
     * @param file
     */
    @ApiOperation(value = "数据导入(多种数据)", notes = "数据导入(多种数据)")
    @PostMapping(value = "/multiple/imp/data")
    public Result multipleImpData(@RequestParam("file") MultipartFile file) {

        ManySheets multiple = ExcelUtil.importMultiple(file, ManySheets.class);
        boolean saveBatch = goodService.saveBatch(multiple.goodsBeanList);
        boolean saveBatch1 = goodService.saveBatch(multiple.goodsBeanList1);
        System.out.println(multiple);
        return ResultUtil.success(1);
    }

    /**
     * 多个工作簿同时导出  没问题
     * @param response
     */
    @ApiOperation(value = "数据导出(多种数据)", notes = "数据导出(多种数据)")
    @GetMapping(value = "/multiple/exp/data")
    public void multipleExpData(HttpServletResponse response) {
        List<GoodsBean> goodsBeanInfoList = goodService.getExcelInfo();
        ManySheets multiple = ManySheets.builder()
                .goodsBeanList(goodsBeanInfoList)
                .goodsBeanList1(goodsBeanInfoList)
                .build();
        String fileName = "多个工作簿导出-用户和商品信息表" + LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));

        ExcelUtil.exportMultiple(fileName, multiple, ManySheets.class, response);
    }

}